package net.tv90.appserveradmin.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import net.tv90.appserveradmin.model.Doctor;
import net.tv90.appserveradmin.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/doctors")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @SaCheckRole("admin")
    @GetMapping
    public String listDoctors(Model model) {
        List<Doctor> doctorsList = doctorService.list();
        model.addAttribute("doctors", doctorsList);
        return "doctors/list";
    }

    @SaCheckRole("admin")
    @GetMapping("/add")
    public String addDoctorForm(Model model) {
        model.addAttribute("doctor", new Doctor());
        return "doctors/add";
    }

    @SaCheckRole("admin")
    @PostMapping("/add")
    public String addDoctor(@ModelAttribute Doctor doctor) {
        doctorService.save(doctor);
        return "redirect:/doctors";
    }

    @SaCheckRole("admin")
    @GetMapping("/edit/{id}")
    public String editDoctorForm(@PathVariable Integer id, Model model) {
        Doctor doctor = doctorService.getById(id);
        model.addAttribute("doctor", doctor);
        return "doctors/edit";
    }

    @SaCheckRole("admin")
    @PostMapping("/edit")
    public String editDoctor(@ModelAttribute Doctor doctor) {
        doctorService.updateById(doctor);
        return "redirect:/doctors";
    }

    @SaCheckRole("admin")
    @GetMapping("/delete/{id}")
    public String deleteDoctor(@PathVariable Integer id) {
        doctorService.removeById(id);
        return "redirect:/doctors";
    }

    @SaCheckRole("admin")
    @GetMapping("/view/{id}")
    public String viewDoctor(@PathVariable Integer id, Model model) {
        Doctor doctor = doctorService.getById(id);
        model.addAttribute("doctor", doctor);
        return "doctors/view";
    }
}
